package view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import model.Set;

import communication.FireAlarm;
import communication.GasAlarm;
import communication.RadiationAlarm;

/**
 * Form panel
 * @author Adrien Ledeul
 * @version 0.1
 */
public class FormPanel extends JPanel implements ActionListener
{
	private static final long serialVersionUID = 1L;
	
	private FormWindow formWindow; 
	private JLabel title;
	private JPanel typePanel;
	private JPanel priorityPanel;
	private JPanel levelPanel;
	private JPanel gasPanel;
	private JComboBox type;
	private ButtonGroup priority;
	private JRadioButton priority1;
	private JRadioButton priority2;
	private JRadioButton priority3;
	private JTextField level;
	private JTextField gas;
	private JButton ok;
	private Set set;

	/**
	 * Constructors
	 * 
	 */
	public FormPanel()
	{
		super();
		this.build();
	}
	
	public FormPanel(FormWindow formWindow)
	{
		super();
		this.formWindow = formWindow;
		this.set = formWindow.getSet();
		this.build();
	}

	/**
	 * Getters & Setters
	 * 
	 */
	public JLabel getTitle() 
	{
		return title;
	}
	
	public FormWindow getFormWindow() 
	{
		return formWindow;
	}

	/**
	 * Build the panel
	 */
	private void build()
	{
		this.setLayout(new GridLayout(6,1));
		
		title = new JLabel("Polytech");
		title.setHorizontalAlignment(JLabel.CENTER);
		
		typePanel = new JPanel();
		typePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		typePanel.add(new JLabel("Type"));
		type = new JComboBox();
		type.setPreferredSize(new Dimension(100,30));
		type.addItem("Fire");
		type.addItem("Gas");
		type.addItem("Radiation");
		type.addActionListener(this);
        typePanel.add(type);
        
		priorityPanel = new JPanel();
		priorityPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		priorityPanel.add(new JLabel("Priority"));
		priority1 = new JRadioButton("1");
		priority1.setSelected(true);
		priority2 = new JRadioButton("2");
		priority3 = new JRadioButton("3");
	    priority = new ButtonGroup();
	    priority.add(priority1);
	    priority.add(priority2);
	    priority.add(priority3);
	    priorityPanel.add(priority1);
	    priorityPanel.add(priority2);
	    priorityPanel.add(priority3);
	    
		levelPanel = new JPanel();
		levelPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		levelPanel.add(new JLabel("Level of Radiation (0-100)"));
		level = new JTextField("0");
		level.setPreferredSize(new Dimension(40,30));
		level.setEnabled(false);
		levelPanel.add(level);
		
		gasPanel = new JPanel();
		gasPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		gasPanel.add(new JLabel("Type of Gas"));
		gas = new JTextField("");
		gas.setPreferredSize(new Dimension(95,30));
		gas.setEnabled(false);
		gasPanel.add(gas);
		
		ok = new JButton("Send");
		ok.addActionListener(this);
		
		this.add(title);
        this.add(typePanel);
        this.add(priorityPanel);
        this.add(levelPanel);
        this.add(gasPanel);
        this.add(ok);
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
        if(e.getSource() == type)
        {
            if(type.getSelectedIndex() == 0)
            {
            	gas.setEnabled(false);
            	level.setEnabled(false);
            }
            else if(type.getSelectedIndex() == 1)
            {
            	gas.setEnabled(true);
            	level.setEnabled(false);
            }
            else if(type.getSelectedIndex() == 2)
            {
            	gas.setEnabled(false);
            	level.setEnabled(true);
            }
        }
        
        if (e.getSource() == ok)
        {
        	
        	int priorityNb = 1;
        	
        	if(priority1.isSelected())
        		priorityNb = 1;
        	else if(priority2.isSelected())
        		priorityNb = 2;
        	else if(priority3.isSelected())
        		priorityNb = 3;
        	
            if(type.getSelectedIndex() == 0)
            {
            	set.getFireSensor().notifyListeners(new FireAlarm(new Date().toString(), title.getText(), priorityNb));
            }
            else if(type.getSelectedIndex() == 1)
            {
            	set.getGasSensor().notifyListeners(new GasAlarm(new Date().toString(), title.getText(), priorityNb, gas.getText()));
            }
            else if(type.getSelectedIndex() == 2)
            {
            	int levelNb = 0;
                try
                {
                	levelNb = Integer.parseInt(level.getText());
	            	if(levelNb < 0 || levelNb >100)
	            		levelNb = 0;
                }
                catch (NumberFormatException ex)
                {
                	levelNb = 0;
                }
                finally
                {
                	set.getRadiationSensor().notifyListeners(new RadiationAlarm(new Date().toString(), title.getText(), priorityNb, levelNb));
                }
            }
        }
	}
}
